% function bigR (Version 1.0)

%references
%   Higgins, M.D., 2006, Quantitative textural measurements in igneous and metamorphic petrology: Cambridge, UK ; New York, Cambridge University Press, ix, 265 p. p.
%   Jerram, D.A., Cheadle, M.J., Hunter, R.H., and Elliott, M.T., 1996, The spatial distribution of grains and crystals in rocks: Contributions to Mineralogy and Petrology, v. 125, p. 60-74.
%   Jerram, D.A., Cheadle, M.J., and Philpotts, A.R., 2003, Quantifying the building blocks of igneous rocks: Are clustered crystal frameworks the foundation?: Journal of Petrology, v. 44, p. 2033-2051.

function funct_bigR=CalcBigR(center,totalArea,area)

%prepare data
    numParticle=size(center,1);
    %totalArea=totalArea;
    %area=area;

%calculate dists of the nearest neighbor
    for i=numParticle:-1:1
        for j=numParticle:-1:1
            tempX=power((center(i,1)-center(j,1)),2);
            tempY=power((center(i,2)-center(j,2)),2);
            dist(i,j)=sqrt(tempX+tempY);
            %dist(i,j)=distance(center(i,:),center(j,:));
            if dist(i,j)==0
                dist(i,j)=NaN;
            end;
        end
    end
    
    R=min(dist);
    
%calculate big R
    Ra=mean(R);
    Na=numParticle/totalArea;
    Re=1/(2*sqrt(Na));
    bigR=Ra/Re;
    porosity=1-(sum(area)/totalArea);
    
    funct_bigR(1,1)=bigR;
    funct_bigR(1,2)=porosity;
    funct_bigR(1,3)=numParticle;